<?php
namespace Home\Controller;
use Think\Controller;
class UserAccountController extends CommonController {
    public function index(){
      header( "content-type:text/html; charset=UTF-8" );
      $rMoney= trim(I('get.rMoney',0,'int'));
      $bankCode= trim(I('get.bankCode'));
      $faceValue=$rMoney;
      if($bankCode==30000){//支付宝
        $channelId=30001;
      }elseif($bankCode==40000){//微信支付
        $channelId=40001;
      }elseif($bankCode==50000){//QQ支付
        $channelId=50001;
      }elseif($bankCode=='ICBC'){//中国工商银行
        $channelId=10001;
      }elseif($bankCode=="ABC"){//中国农业银行
        $channelId=10002;
      }elseif($bankCode=="CCB"){//建设银行
        $channelId=10003;
      }elseif($bankCode=="BOC"){//中国银行
        $channelId=10004;
      }elseif($bankCode=="CMBC"){//中国招商银行
        $channelId=10007;
      }elseif($bankCode=="CMBCS"){//民生银行
        $channelId=10009;
      }elseif($bankCode=="BOCOM"){//交通银行
        $channelId=10005;
      }elseif($bankCode=="CEBBANK"){//光大银行
        $channelId=10010;
      }elseif($bankCode=="PSBC"){//中国邮政
        $channelId=10017;
      }elseif($bankCode=="HXB"){//华夏银行
        $channelId=10018;
      }elseif($bankCode=="SPDB"){//上海浦东发展银行
        $channelId=10016;
      }elseif($bankCode=="CGB"){//广东发展银行
        $channelId=10013;
      }

$gateWary="/pay/i.php";
$eMail='';
$payId='';
$payKey='';
$retUrl="http://".$_SERVER["HTTP_HOST"]."/Callback";

$orderId=getOrderId();
$payMode=$channelId;
$money=$rMoney;

$signstr='eMail='.$eMail.'&payId='.$payId.'&payKey='.$payKey.'&orderId='.$orderId.'&payMode='.$payMode.'&retUrl='.$retUrl.'&money='.$money;

$sign=strtoupper(md5($signstr));

$params="eMail=".$eMail;
$params.="&payMode=".$payMode;
$params.="&orderId=".$orderId;
$params.="&money=".$money;
$params.="&retUrl=".$retUrl;
$params.="&sign=".$sign;

$time=time();
$data2['uid']=$_SESSION ['uid'];
$data2['orderid']=$orderId;
$data2['bankid']=$payMode;
$data2['realmoney']=$money;
if (C('PAY_ADD_ON') && $money>=C('PAY_AMONT')){
	$gmoney=C('CHA_GMONERY')+C('CHA_GMONERY')*C('PAY_ADD');
	$data2['gamemoney']=$money*$gmoney;
}else{
	$data2['gamemoney']=$money*C('CHA_GMONERY');
}
$data2['creattime']=$time;

$result1=M('withpay')->data($data2)->add();

if($result1){
  header("location:$gateWary?$params");
}
    }



    public function transferAccounts_done() {
        $username = trim(I('post.username', '', 'htmlspecialchars'));
        $type = trim(I('post.type', 0, 'int'));
        $sum = trim(I('post.money', 0, 'int'));
        if($sum <= 0){
            $data['Success'] = false;
            $data['Obj'] = "请输入转账金额";
            $this->ajaxReturn($data);
        }
        if (strlen($username) < 1) return;
        if ($type == 2) {

        } else if ($type == 1) {
          // 转账
            $list = M('user')->where(array(
                  'Id' => $_SESSION['uid']
              ))->find();
              if ($sum > $list['monery']) {
                  $data['Success'] = false;
                  $data['Obj'] = '账户余额不足';
                  $this->ajaxReturn($data);
            }
            $userinfo = M('user')->where(array('username' => $username))->find();
            if(!$userinfo){
              $data['Success'] = false;
              $data['Obj'] = "对方账号不存在";
              $this->ajaxReturn($data);
            }
            $addtime = date('Y-m-d H:i', time());
            $remark = "转账：从".$list[nicename]."转出金额".$sum."至".$userinfo[nicename];

            M('user')->startTrans();
            $result = M('user')->where(array('Id' => $_SESSION['uid']))->setDec('monery', $sum);
            if (!$result) {
              M('user')->rollback();$data['Success'] = false;$data['Obj'] = '转账失败!';$this->ajaxReturn($data);
            }
            $result = M('user')->where(array('Id' => $userinfo['id']))->setInc('monery', $sum);
            if (!$result) {
              M('user')->rollback();$data['Success'] = false;$data['Obj'] = '转账失败!'.$userinfo['id'];$this->ajaxReturn($data);
            }
            $data2 = array('from_user_id' => $_SESSION['uid'],'to_user_id' => $userinfo['id'],'addtime' => $addtime,'remark' => $remark,'amount' => $sum);
            $result = M('account_log')->data($data2)->add();
            if (!$result) {
              M('user')->rollback();$data['Success'] = false;$data['Obj'] = '转账失败!';$this->ajaxReturn($data);
            }
            M('user')->commit();
            $data['Success'] = true;
            $data['Obj'] = "转账成功";
        } else {
            return;
        }
        $this->ajaxReturn($data);
    }

}
